﻿using AutoMapper;
using BS.Domain.Cultivation;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Write.Api.Handler.Imunities
{
    public class UpdateImunityCommandHandler : IRequestHandler<UpdateImunityCommand, ApiResult>
    {
        private readonly IImunitysRepository _immunitysRepository;

        private readonly ILogger<UpdateImunityCommandHandler> _logger;

        private readonly IMapper _mapper;

        public UpdateImunityCommandHandler(IImunitysRepository immunitysRepository, ILogger<UpdateImunityCommandHandler> logger, IMapper mapper)
        {
            _immunitysRepository = immunitysRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult> Handle(UpdateImunityCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var immunities = _mapper.Map<Imunitys>(request);
                var sio = await _immunitysRepository.UpdateAsync(immunities);
                return ApiResult.Ok(sio);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "UpdateImunityCommandHandler");
                return ApiResult.Fail(ex.Message);
            }
        }
    }
}
public class UpdateImunityCommand:IRequest<ApiResult>
{
    public int ImunityId { get; set; }
    /// <summary>
    /// 饲养天数
    /// </summary>
    public int RearingDays { get; set; }
    /// <summary>
    /// 免疫方式
    /// </summary>
    public string ImunityWay { get; set; } = string.Empty;
    /// <summary>
    /// 免疫方法
    /// </summary>
    public string ImunityMethods { get; set; } = string.Empty;
    /// <summary>
    /// 养殖信息ID
    /// </summary>
    public int BreedingId { get; set; }
}